package com.legal.aid.app.controller;

import com.legal.aid.app.domain.dto.CommentAddDto;
import com.legal.aid.app.domain.dto.CommentDto;
import com.legal.aid.app.domain.dto.CommentLikeDto;
import com.legal.aid.app.domain.vo.CommentVo;
import com.legal.aid.app.service.IAppCommentService;
import com.legal.aid.common.core.controller.BaseController;
import com.legal.aid.common.core.domain.AjaxResult;
import com.legal.aid.common.core.domain.model.AppLoginUser;
import com.legal.aid.common.core.page.TableDataInfo;
import com.legal.aid.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/comment")
public class AppCommentController extends BaseController {

    @Autowired
    private IAppCommentService commentService;

    /**
     * 查询评论列表 targetType 分为news case
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("targetId") String targetId,
            @RequestParam("targetType") String targetType) {
        startPage();
        CommentDto commentDto = new CommentDto();
        commentDto.setTargetId(targetId);
        String targetStr = targetType.equals("news") ? "1" : "2";
        commentDto.setTargetType(targetStr);
        List<CommentVo> list = commentService.selectCommentList(commentDto);
        return getDataTable(list);
    }

    /**
     * 新增评论
     */
    @PostMapping
    public AjaxResult add(@RequestBody CommentAddDto commentAddDto) {
        // 获取当前登录用户ID
        AppLoginUser appLoginUser = SecurityUtils.getAppLoginUser();
        return toAjax(commentService.insertComment(commentAddDto, appLoginUser.getUserId()));
    }

    /**
     * 评论点赞
     * 
     * @param commentLikeDto 目标id ，目标类型
     * @return
     */
    @PutMapping("/like")
    public AjaxResult like(@RequestBody CommentLikeDto commentLikeDto) {
        return success(commentService.likeComment(commentLikeDto));
    }
}
